package com.ejd.web.vo.customer;

import java.util.ArrayList;




import java.util.List;
import java.util.Locale;


import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import net.sf.cglib.beans.BeanCopier;

import com.ejd.service.ICustomerService;
import com.ejd.service.impl.CustomerService;
import com.ejd.utils.DESPlusUtil;
import com.ejd.utils.EncryptUtils;
import com.ejd.utils.MessageFactory;
import com.ejd.utils.SpringFacesUtil;
import com.ejd.web.bo.impl.Customer;
import com.ejd.web.constant.EjdConstants;
import com.ejd.web.constant.ManageBeanConstants;
import com.ejd.web.constant.SpringBeanConstants;
import com.ejd.web.vo.common.CustomerVo;
import com.ejd.web.vo.genl.GenerateVerifyCodeBean;

@ManagedBean(name="createCustomerPage")
@SessionScoped
public class CreateCustomerPageBean extends CustomerBaseBean<Customer,Long> implements java.io.Serializable {
	private static final long serialVersionUID = -8106629465344224753L;
	private CustomerVo customer = new CustomerVo();
	private List<String> errorMessages = new ArrayList<String>();
	private boolean agreeFlag = false;
	private String verifyCodeForQuestion;
	private String verifyCode;
	
	public CustomerVo getCustomer() {
		return customer;
	}

	public void setCustomer(CustomerVo customer) {
		this.customer = customer;
	}
	
	public List<String> getErrorMessages() {
		return errorMessages;
	}

	public void setErrorMessages(List<String> errorMessages) {
		this.errorMessages = errorMessages;
	}

	public boolean isAgreeFlag() {
		return agreeFlag;
	}

	public void setAgreeFlag(boolean agreeFlag) {
		this.agreeFlag = agreeFlag;
	}

	public String getVerifyCodeForQuestion() {
		return verifyCodeForQuestion;
	}

	public void setVerifyCodeForQuestion(String verifyCodeForQuestion) {
		this.verifyCodeForQuestion = verifyCodeForQuestion;
	}

	public String getVerifyCode() {
		return verifyCode;
	}

	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}

	public CreateCustomerPageBean(){
		// GenerateVerifyCode
		GenerateVerifyCodeBean generateVerifyCode = (GenerateVerifyCodeBean) SpringFacesUtil.getManagedBean(ManageBeanConstants.GENERATE_VERIFY_CODE_BEAN);
		this.setVerifyCodeForQuestion(generateVerifyCode.getVerifyCode());
		// end generateVerifyCode
	}
	public String generateVerifyCode() {
		GenerateVerifyCodeBean generateVerifyCode = (GenerateVerifyCodeBean) SpringFacesUtil.getManagedBean(ManageBeanConstants.GENERATE_VERIFY_CODE_BEAN);
		this.setVerifyCodeForQuestion(generateVerifyCode.getVerifyCode());
		return null;
	}
	public String saveCustomer() {
		this.getErrorMessages().clear();
		ICustomerService<Customer,Long> customerService = (ICustomerService<Customer,Long>) SpringFacesUtil.getSpringBean(SpringBeanConstants.CUSTOMER_SERVICE_BEAN_NAME);
		customerService.setEntityClass(Customer.class);
		customerService.setPKClass(Long.class);
		Customer newCustomer = new Customer();
		BeanCopier copyHere = BeanCopier.create(CustomerVo.class,Customer.class,false);
		copyHere.copy(this.getCustomer(), newCustomer, null);
		Customer customer = customerService.findUniqueBy("customerId", this.getCustomer().getCustomerId());
		
		if (null != customer) {
			this.getErrorMessages().add(MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"customerId.exist.error.message", FacesMessage.SEVERITY_ERROR, this.getCustomer().getCustomerId()).getDetail());
		}
		String email = (String) this.getCustomer().getEmail();
		String verifyEmail = (String) this.getCustomer().getVerifyEmail();
		if (!(email.equals(verifyEmail))) {
			this.getErrorMessages().add(MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"email.notmatch.error.message", FacesMessage.SEVERITY_ERROR).getDetail());
		}
		String password = (String) this.getCustomer().getPassword();
		String verifyPassword = (String) this.getCustomer().getVerifyPassword();
		if (!(password.equals(verifyPassword))) {
			this.getErrorMessages().add(MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"password.notmatch.error.message", FacesMessage.SEVERITY_ERROR).getDetail());
		}
		if (!(this.getVerifyCodeForQuestion().toUpperCase().equals(this.getVerifyCode().toUpperCase()))) {
			this.getErrorMessages().add("验证码输入错误");
		}
		if (this.getErrorMessages().size() > 0) {
			return null;
		}
		String enPassword = EncryptUtils.encryptMD5(password);
		newCustomer.setPassword(enPassword);
		customerService.save(newCustomer);
		//re generate verifycode
		GenerateVerifyCodeBean generateVerifyCode = (GenerateVerifyCodeBean) SpringFacesUtil.getManagedBean(ManageBeanConstants.GENERATE_VERIFY_CODE_BEAN);
		this.setVerifyCodeForQuestion(generateVerifyCode.getVerifyCode());
		//end
		String tempCustomerId = this.getCustomer().getCustomerId();
		//clear data for input
		this.getCustomer().setCustomerId("");
		this.getCustomer().setEmail("");
		this.getCustomer().setVerifyEmail("");
		this.getCustomer().setPassword("");
		this.getCustomer().setVerifyPassword("");
		this.setVerifyCode("");
		this.setAgreeFlag(false);
		//end
		return "registerSuccess" + "?faces-redirect=true&customerId=" + tempCustomerId + "&param=" + enPassword;
	}
}
